package com.example.commen.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Process;
import android.util.DisplayMetrics;

import androidx.core.content.ContextCompat;
import androidx.multidex.MultiDex;

import com.blankj.utilcode.util.CrashUtils;
import com.example.commen.BuildConfig;
import com.example.commen.R;
import com.example.commen.manager.ActivityManager;
import com.example.commen.other.TitleBarStyle;
import com.example.commen.other.ToastStyle;
import com.hjq.bar.TitleBar;
import com.hjq.toast.Toaster;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.tencent.bugly.crashreport.CrashReport;
import com.wx.android.common.util.SharedPreferencesUtils;

import java.lang.reflect.Method;



/**
 * Created by Allen on 2018/8/16.
 */
public class UCApplication extends Application {
    /**
     * 屏幕宽度
     */
    private static int screenWidth;
    /**
     * 屏幕高度
     */
    private static int screenHeight;

    private static Context context;

    private static Handler mHandler;

    /**
     * 主线程id
     */
    private static int mianThreadId;


    private static UCApplication application = null;

    private String BuglyAppid = "";

    /**
     * 需要在onCreate之前调用
     *
     * @param buglyAppid
     */
    public void setBuglyAppid(String buglyAppid) {
        BuglyAppid = buglyAppid;
    }

    public static int width = 540;
    public static int height = 1200;


    @Override
    public void onCreate() {
        super.onCreate();
        initBase();
        //设置activity前后台监听
//        registerActivityLifecycleCallbacks(new MyActivityLifecycleCallbacks());
    }

    private void initBase() {
        //计算屏幕宽高
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        screenWidth = metrics.widthPixels;
        screenHeight = metrics.heightPixels;

        context = getApplicationContext();
        mHandler = new Handler();
        mianThreadId = Process.myTid();
        application = this;
        MultiDex.install(this);
//        initAutoSize();
        // Activity 栈管理初始化
        ActivityManager.getInstance().init(application);

        Toaster.init(this, new ToastStyle());
        Toaster.setDebugMode(BuildConfig.DEBUG);
        SharedPreferencesUtils.init(this);


        if (!BuglyAppid.isEmpty()) {
            CrashReport.initCrashReport(getApplicationContext(), BuglyAppid, false);
            CrashUtils.init(crashInfo -> {
                CrashReport.postCatchedException(crashInfo.getThrowable());
            });
        }
    }


    public static UCApplication getBaseApplication() {
        return application;
    }


    /**
     * .获取顶部statusBar高度
     *
     * @return
     */
    private int getStatusBarHeight() {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    /**
     * 获取底部navigationBar高度
     *
     * @return
     */
    private int getNavigationBarHeight() {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    /**
     * 获取设备是否存在NavigationBar
     *
     * @return
     */
    public static boolean checkDeviceHasNavigationBar() {
        boolean hasNavigationBar = false;
        Resources rs = context.getResources();
        int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id);
        }
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                hasNavigationBar = true;
            }
        } catch (Exception e) {
            //do something
        }
        return hasNavigationBar;
    }


    public static Context getContext() {
        return context;
    }

    public static Handler getHandler() {
        return mHandler;
    }

    public static int getMainThreadId() {
        return mianThreadId;
    }

    public static int getScreenWidth() {
        return screenWidth;
    }

    public static int getScreenHeight() {
        return screenHeight;
    }


    public static Activity getCurrentActivity() {
        return ActivityManager.getInstance().getTopActivity();
    }


    static {

        // 设置标题栏初始化器
        TitleBar.setDefaultStyle(new TitleBarStyle());

        // 设置全局的 Header 构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((cx, layout) ->
                new MaterialHeader(application).setColorSchemeColors(ContextCompat.getColor(application, R.color.common_accent_color)));
        // 设置全局的 Footer 构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((cx, layout) -> new ClassicsFooter(application));
        // 设置全局初始化器
        SmartRefreshLayout.setDefaultRefreshInitializer((cx, layout) -> {
            // 刷新头部是否跟随内容偏移
            layout.setEnableHeaderTranslationContent(true)
                    // 刷新尾部是否跟随内容偏移
                    .setEnableFooterTranslationContent(true)
                    // 加载更多是否跟随内容偏移
                    .setEnableFooterFollowWhenNoMoreData(true)
                    // 内容不满一页时是否可以上拉加载更多
                    .setEnableLoadMoreWhenContentNotFull(true)
                    // 仿苹果越界效果开关
                    .setEnableOverScrollDrag(true);
        });
    }
}
